package cn.laixueit.controller;

import cn.laixueit.pojo.Brand;
import cn.laixueit.result.PageResult;
import cn.laixueit.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/page")
    public ResponseEntity<PageResult> page(@RequestParam(value = "key",required = false)String key,
                                           @RequestParam("page")Integer page,
                                           @RequestParam("rows")Integer rows,
                                           @RequestParam(value = "sortBy",required = false)String sortBy,
                                           @RequestParam(value = "desc",required = false)Boolean desc){
        PageResult result =  brandService.page(key,page,rows,sortBy,desc);
        return  ResponseEntity.ok(result);
    }

    @PostMapping
    public ResponseEntity<Void> save(@RequestBody Brand brand,@RequestParam("cids")Long[] cids){
        brandService.saveBrandAndCategory(brand,cids);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> brandListByCid(@PathVariable("cid")Long cid){
        List<Brand> brandList = brandService.brandListByCid(cid);
        return ResponseEntity.ok(brandList);
    }

    @GetMapping("/getBrandById/{id}")
    public ResponseEntity<Brand> getBrandById(@PathVariable("id")Long id){
        Brand brand = brandService.getById(id);
        return ResponseEntity.ok(brand);
    }

    @GetMapping("/findBrandList")
    public ResponseEntity<List<Brand>>  findBrandList(@RequestParam List<Long> bids){
         List<Brand> brandList = brandService.findBrandList(bids);
         return  ResponseEntity.ok(brandList) ;
    }
}
